var express = require("express");
var router = express.Router();
const Db = require("../utils/db");
const createSql = require("../utils/Create_SQL");
const fs = require('fs');
const path = require('path');
const { errorCode, successCode, config } = require('../config/config');
const utils = require('../utils/utils.js');


/**
 * @swagger
 * /api/biographicalNotes/getUserInfo:
 *    get:
 *      tags:
 *      - 简历接口
 *      summary: 获取个人信息
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: type
 *          in: query
 *          description: 详细信息
 *          type: string
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getUserInfo", async (req, res) => {
    let query = req.query;
    let sql = createSql.select().from("my_userinfo").where(`is_show = 1 AND is_del != 1`).end();
    let result = await Db.query(sql, res);
    if (!result.length) {
        return res.json({
            ...successCode[200],
            data: {}
        });
    }
    let data = result[0];
    if (query.type != 'zsduan') {
        data.tel = data.tel.substr(0, 7) + "****";
    }
    let userIconPath = path.resolve(__dirname, '../upload/' + data.usericon);
    try {
        let userIconFs = fs.readFileSync(userIconPath);
        data.usericon = "data:image/jpeg;base64," + Buffer.from(userIconFs, 'binary').toString('base64');
    } catch (error) {
        data.usericon = "";
    }
    data.addtime = utils.formatDate(data.addtime);
    return res.json({
        ...successCode[200],
        data: data
    });
});

/**
 * @swagger
 * /api/biographicalNotes/getEvaluate:
 *    get:
 *      tags:
 *      - 简历接口
 *      summary: 获取自我评价
 *      produces:
 *      - application/json
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getEvaluate", async (req, res) => {
    let sql = createSql.select().from("my_evaluation").where(`is_show = 1 AND is_del != 1`).end();
    let result = await Db.query(sql, res);
    if (!result.length) {
        return res.json({
            ...successCode[200],
            data: []
        });
    }
    result.forEach(item => {
        item.addtime = utils.formatDate(item.addtime);
    });
    return res.json({
        ...successCode[200],
        data: result[0]
    });
});


/**
 * @swagger
 * /api/biographicalNotes/getEducation:
 *    get:
 *      tags:
 *      - 简历接口
 *      summary: 获取我的教育信息
 *      produces:
 *      - application/json
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getEducation", async (req, res) => {
    let sql = createSql.select().from("my_school").where(`is_show = 1 AND is_del != 1`).end();
    let result = await Db.query(sql, res);
    if (!result.length) {
        return res.json({
            ...successCode[200],
            data: []
        });
    }
    result.forEach(item => {
        item.addtime = utils.formatDate(item.addtime);
    });
    return res.json({
        ...successCode[200],
        data: result
    });
})


/**
 * @swagger
 * /api/biographicalNotes/getAbility:
 *    get:
 *      tags:
 *      - 简历接口
 *      summary: 获取我的能力
 *      produces:
 *      - application/json
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getAbility", async (req, res) => {
    let sql = createSql.select().from("my_abilitys").where(`is_show = 1 AND is_del != 1`).order("sorts").end();
    let result = await Db.query(sql, res);
    if (!result.length) {
        return res.json({
            ...successCode[200],
            data: []
        });
    }
    result.forEach(item => {
        item.addtime = utils.formatDate(item.addtime);
    });
    return res.json({
        ...successCode[200],
        data: result
    });
})

/**
 * @swagger
 * /api/biographicalNotes/getProject:
 *    get:
 *      tags:
 *      - 简历接口
 *      summary: 获取我的项目
 *      produces:
 *      - application/json
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getProject", async (req, res) => {
    let sql = createSql.select().from("my_project").where(`is_show = 1 AND is_del != 1`).order("sorts").end();
    let result = await Db.query(sql, res);
    if (!result.length) {
        return res.json({
            ...successCode[200],
            data: []
        });
    }
    result.forEach(item => {
        item.addtime = utils.formatDate(item.addtime);
    });
    return res.json({
        ...successCode[200],
        data: result
    });
});

/**
 * @swagger
 * /api/biographicalNotes/getPrintCss:
 *    get:
 *      tags:
 *      - 简历接口
 *      summary: 获取打印相关css
 *      produces:
 *      - application/json
 *      responses:
 *        200:
 *         description: successful operation
 * */
router.get("/getPrintCss", async (req, res) => {
    let cssPath = path.resolve(__dirname, '../public/stylesheets/aboutme.min.css');
    let cssFs = fs.readFileSync(cssPath , 'utf-8');
    return res.json({
        ...successCode[200],
        data: cssFs
    })
})

module.exports = router;